package com.xue.springboot001.controller;

import com.xue.springboot001.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import javax.servlet.http.HttpSession;

@Controller
public class LoginController {

    @GetMapping(value = {"/login","/"})
    public String login(){
        return "login";
    }

    @PostMapping("/login")
    public String main(User user, HttpSession session, Model model){
        if (StringUtils.hasLength(user.getUsername()) && "123".equals(user.getPassword())){
            session.setAttribute("loginUser",user);
            return "redirect:/main";
        }else {
            model.addAttribute("msg","账号密码错误");
            return "login";
        }
    }

    @GetMapping("/main")
    public String main(HttpSession session, Model model){
//        if (session.getAttribute("loginUser") !=null){
//            return "main";
//        }else {
//            model.addAttribute("msg","请重新登录!");
//            return "login";
//        }
        return "main";
    }





}
